using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource.Analysis;
using UCL.Powersource.Statistics.CodeIndex;

namespace UCL.Powersource.Statistics.CodeIndex
{
    [Export(typeof(IStatistic))]
    [DataContract(IsReference = true)]
    public abstract class CodeIndexStatistic : StatisticTeamBase
    {
        
        protected Calculator _calculator;
        protected StatisticTypeValue _StatisticType;

        /// <summary>
        /// The ValueType Property can only be set by a subclass's constructor.
        /// The user instantiating one of this class' subsclasses defines wich
        /// value the class is instantiated with; it cannot be changed afterwards.
        /// </summary>
        protected virtual StatisticTypeValue ValueType
        {
            private set;
            get;
        }

        protected virtual StatisticDefinition StatisticDef
        {
            private set;
            get;
        }

        //protected abstract StatisticTypeValue StatisticType;

        /// <summary>
        /// This constructor needs to be overriden by any class extending this 
        /// class.
        /// </summary>
        /// <param name="calculatorFactory">
        /// This is reference to the calculatorFactory, which implements the 
        /// singleton pattern. The method to get an instance of a particular 
        /// calculator depends on the type of the subclass.
        /// </param>
        /// <param name="typeValue">
        /// This parameter specifies the type of the statistic being calculated
        /// ranging in the following values: 
        ///     Maximum, Minumum, Average, Median or UniqueValue
        /// </param>
        protected CodeIndexStatistic(StatisticTypeValue typeValue)
        {
            ValueType = typeValue;
        }

        /// <summary>
        /// This abstract class overrides the abstract CalculateFor method 
        /// defined in the StatisticTeamBase superclass. Here, the respon-
        /// sibility of calculating the statistic is delegated to te class' 
        /// specific instantiation of a Calculator.
        /// </summary>
        protected override double? CalculateFor(ITfsTeamProjectConfiguration teamProjConfig, ITimeFrame timeFrame)
        {
            if (_calculator == null)
            {
                _calculator = CalculatorFactory.getCalculator(StatisticDef);
            }

            return _calculator.calculateStatistic(teamProjConfig, timeFrame, ValueType);
        }

        public override bool RequiresBuild
        {
            get { return true; }
        }
    }

    /// <remarks>
    /// This is a specialization of the CodeIndexStatistic class.
    /// The calculator factory defines what behaviour this class will display
    /// through the calculator assigned inside this class' constructor.
    /// 
    /// In this way, the user does not have to worry about which Calculator to 
    /// use, just instantiates a class depending on the statistic to calculate
    /// and specifies the type value to calculate.
    /// 
    /// This specialization of the CodeIndexStatistic class asks the calculator
    /// referenced received as a parameter of its constructor to return the
    /// reference to the RFCCalculator.
    /// </remarks>
   

   
}
